﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using SCRUM.Core.BusinessObjects;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Privider for comunication with the database for daily scrum
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Returns all deatils of daily scrum meetings for user
        /// </summary>
        /// <param name="user">Specific user</param>
        /// <returns>List of daily scrum details</returns>
        public override List<DailyScrum> GetAlldailyScrumDetails(User user)
        {
            List<DailyScrum> result = new List<DailyScrum>();
            DbParameter dbUserID = CreateParameter("userID", user.ID);

            try
            {
                connection.Open();
                DbDataReader data = GetGrid("dailyScrum/GetAllDailyScrumDetails", dbUserID);
                while (data.Read())
                    {
                        DailyScrum ds = new DailyScrum(GetIntByColumn(data, "ID"),
                                                       GetDateByColumn(data, "DATE"),
                                                       user);
                        ds.Did = GetStringByColumn(data, "DID");
                        ds.ToDo = GetStringByColumn(data, "TO_DO");
                        ds.Problems = GetStringByColumn(data, "PROBLEMS");
                        ds.Comments = GetStringByColumn(data, "COMMENTS");
                        ds.StoryPoints = GetDoubleByColumn(data, "STORY_POINTS");
                        int taskID = GetIntByColumn(data, "TASK_ID");
                        ds.TaskID = taskID;
                        result.Add(ds);
                    }
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Creates new DailyScrum detail
        /// </summary>
        /// <param name="ds">DailyScrum to create</param>
        /// <returns>Created DailyScrum with ID set</returns>
        public override DailyScrum CreateNewDailyScrumDetail(DailyScrum ds)
        {
            DbParameter dbDay = CreateParameter("day", ds.Date);
            DbParameter dbDid = CreateParameter("did", ds.Did);
            DbParameter dbToDo = CreateParameter("toDo", ds.ToDo);
            DbParameter dbProblems = CreateParameter("problems", ds.Problems);
            DbParameter dbComments = CreateParameter("comments", ds.Comments);
            DbParameter dbTaskID = CreateParameter("taskID", ds.Task.ID);
            DbParameter dbUserID = CreateParameter("userID", ds.User.ID);
            DbParameter dbBurnDown = CreateParameter("burnDown", ds.StoryPoints);

            try
            {
                connection.Open();
                int data = ExecuteNonQuery("dailyScrum/InsertDailyScrum", dbDay, dbDid, dbToDo,dbProblems, dbComments, dbTaskID, dbUserID, dbBurnDown);
                if (data != 1)
                {
                    throw new Exception("Daily scrum NOT created!");
                }
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            return FindDailyScrumDetailID(ds);
        }

        /// <summary>
        /// Gets DailyScrum detail with Id
        /// </summary>
        /// <param name="ds">DailyScrum to find Id for</param>
        /// <returns>DailyScrum with ID set</returns>
        public DailyScrum FindDailyScrumDetailID(DailyScrum ds)
        {
            DbParameter dbDay = CreateParameter("day", ds.Date);
            DbParameter dbDid = CreateParameter("did", ds.Did);
            DbParameter dbToDo = CreateParameter("toDo", ds.ToDo);
            DbParameter dbProblems = CreateParameter("problems", ds.Problems);
            DbParameter dbComments = CreateParameter("comments", ds.Comments);
            DbParameter dbTaskID = CreateParameter("taskID", ds.Task.ID);
            DbParameter dbUserID = CreateParameter("userID", ds.User.ID);
            DbParameter dbBurnDown = CreateParameter("burnDown", ds.StoryPoints);

            try
            {
                connection.Open();
                DbDataReader data = GetVector("dailyScrum/GetDailyScrumsID");
                if (data.Read())
                {
                    ds.ID = GetIntByColumn(data, "ID");
                }
                else 
                {
                    throw new Exception("Daily scrum NOT created!");
                }
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            return ds;
        }

        /// <summary>
        /// Updates DailyScrum
        /// </summary>
        /// <param name="ds">Daily scrum to update</param>
        public override void UpdateDailyScrum(DailyScrum ds)
        {
            DbParameter dbID = CreateParameter("id", ds.ID);
            DbParameter dbDay = CreateParameter("day", ds.Date);
            DbParameter dbDid = CreateParameter("did", ds.Did);
            DbParameter dbToDo = CreateParameter("toDo", ds.ToDo);
            DbParameter dbProblems = CreateParameter("problems", ds.Problems);
            DbParameter dbComments = CreateParameter("comments", ds.Comments);
            DbParameter dbTaskID = CreateParameter("taskID", ds.Task.ID);
            DbParameter dbUserID = CreateParameter("userID", ds.User.ID);
            DbParameter dbBurnDown = CreateParameter("burnDown", ds.StoryPoints);

            try
            {
                connection.Open();
                int data = ExecuteNonQuery("dailyScrum/UpdateDailyScrum", dbID, dbDay, dbDid, dbToDo,dbProblems, dbComments, dbTaskID, dbUserID, dbBurnDown);
                if (data != 1)
                {
                    throw new Exception("Daily scrum NOT updated!");
                }
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
        }

        /// <summary>
        /// Deletes daily scrum detail entry
        /// </summary>
        /// <param name="dsID">ID of the detail</param>
        public override void DeleteDailyScrum(int dsID)
        {
            DbParameter dbID = CreateParameter("id", dsID);

            try
            {
                connection.Open();
                int data = ExecuteNonQuery("dailyScrum/DeleteDailyScrum", dbID);
                if (data != 1)
                {
                    throw new Exception("Daily scrum NOT deleted!");
                }
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                }
            }
        }
    }
}
